
#include <iostream>
#include <string.h>
#include <stdbool.h>
#include <assert.h>

using namespace std;

namespace my_string
{
    class string 
    {
    public:
        typedef char* iterator;
        typedef const char* const_iterator;

        iterator begin()
        {
            return _str;
        }
        const_iterator begin() const 
        {
            return _str;
        }
        iterator end()
        {
            return _str + _size;
        }
        const_iterator end() const 
        {
            return _str + _size;
        }

        //string(const char* str = "")
        //    :_str(nullptr)
        //     ,_size(strlen(str))
        //     ,_capacity(strlen(str))
        //{
        //    _str = new char[_capacity + 1];
        //    strcpy(_str, str);
        //}

        string(const char* str = "")
        {
            _size = strlen(str);
            _capacity = _size;
            _str = new char[_capacity + 1];

            strcpy(_str, str);
        }

        string(size_t n, char ch)
            :_str(new char[n + 1])
             ,_size(n)
             ,_capacity(n)
        {
            for (size_t i = 0; i < _size; i ++ )
            {
                _str[i] = ch;
            }
        }

        ~string()
        {
            delete[] _str;

            _str = nullptr;
            _size = _capacity = 0;
        }
        
        void swap(string& s)
        {
            ::swap(_str, s._str);
            ::swap(_size, s._size);
            ::swap(_capacity, s._capacity);
        }

        // 传统写法
        //string(const string& s)
        //    :_size(s._size)
        //     ,_capacity(s._capacity)
        //{
        //    _str = new char[_capacity + 1];
        //    strcpy(_str, s._str);
        //}
        
        // 现代写法
        string(const string& s)
            :_str(nullptr)
             ,_size(0)
             ,_capacity(0)
        {
            string tmp(s._str);
            swap(tmp);
        }

        string& operator=(string s)
        {
            swap(s);

            return *this;
        }

        size_t size() const 
        {
            return _size;
        }

        bool empty() const 
        {
            return _size == 0;
        }

        char& operator[](size_t pos)
        {
            assert(pos < _size);

            return _str[pos];
        }
        const char& operator[](size_t pos) const 
        {
            assert(pos < _size);

            return _str[pos];
        }

        const char* c_str() const
        {
            return _str;
        }

        void reserve(size_t n)
        {
            if (n > _capacity)
            {
                char* tmp = new char[n + 1];
                strcpy(tmp, _str);
                delete[] _str;

                _str = tmp;
                _capacity = n;
            }
        }

        void push_back(char ch)
        {
            if (_size == _capacity)
            {
                reserve(_capacity == 0 ? 4 : _capacity * 2);
            }

            _str[_size ++ ] = ch;
            _str[_size] = '\0';
        }

        void append(const char* str)
        {
            size_t len = strlen(str);

            if (_size + len > _capacity)
            {
                reserve(_size + len);
            }

            strcpy(_str + _size, str);

            _size += len;
        }   
        void append(size_t n, char ch)
        {
            reserve(_size + n);

            for (size_t i = 0; i < n; i ++ )
            {
                push_back(ch);
            }
        }
        void append(const string& s)
        {
            append(s._str);
        }

        string& operator+=(char ch)
        {
            push_back(ch);

            return *this;
        }
        string& operator+=(const string& s)
        {
            append(s);

            return *this;
        }
        string& operator+=(const char* str)
        {
            append(str);

            return *this;
        }

        string& insert(size_t pos, char ch)
        {
            assert(pos <= _size);

            if (_size == _capacity)
            {
                reserve(_capacity == 0 ? 4 : _capacity * 2);
            }

            size_t end = _size + 1;
            while (end > pos)
            {
                _str[end] = _str[end - 1];
                end -- ;
            }

            _str[pos] = ch;
            ++ _size;

            return *this;
        }
        string& insert(size_t pos, const char* str)
        {
            assert(pos <= _size);

            size_t len = strlen(str);

            if (len == 0)
            {
                return *this;
            }

            if (_size + len > _capacity)
            {
                reserve(_size + len);
            }

            size_t end = _size + len;
            while (end >= pos + len)
            {
                _str[end] = _str[end - len];
                -- end;
            }

            strncpy(_str + pos, str, len);
            _size += len;

            return *this;
        }

        void erase(size_t pos, size_t len = npos)
        {
            assert(pos < _size);

            if (len == npos || pos + len >= _size)
            {
                _str[pos] = '\0';
                _size = pos;
            }
            else 
            {
                strcpy(_str + pos, _str + pos + len);
                _size -= len;
            }
        }

        size_t find(char ch, size_t pos = 0) const 
        {
            assert(pos < _size);

            for (size_t i = pos; i < size(); i ++ )
            {
                if (_str[i] == ch)
                {
                    return i;
                }
            }

            return npos;
        }

        size_t find(const char* sub, size_t pos = 0) const 
        {
            assert(pos < _size);
            
            const char* ret = strstr(_str + pos, sub);

            if (ret == NULL)
            {
                return npos;
            }

            return ret - _str;
        }

        bool operator==(const string& s) const 
        {
            return strcmp(_str, s._str) == 0;
        }

        bool operator!=(const string& s) const 
        {
            return !(*this == s);
        }

        bool operator<(const string& s) const 
        {
            return strcmp(_str, s._str) < 0;
        }

        bool operator<=(const string& s) const 
        {
            return *this < s || *this == s;
        }

        bool operator>(const string& s) const 
        {
            return !(*this <= s);
        }

        bool operator>=(const string& s) const 
        {
            return *this > s || *this == s;
        }

    private:
        char* _str;
        size_t _size;
        size_t _capacity;

        const static size_t npos = -1;
    };

    ostream& operator<<(ostream& out, const string& s)
    {
        for (size_t i = 0; i < s.size(); i ++ )
        {
            out << s[i];
        }

        return out;
    }
    istream& operator>>(istream& in, string& s)
    {
        char ch;
        ch = in.get();

        while (ch != ' ' && ch != '\n')
        {
            s += ch;
            ch = in.get();
        }

        return in;
    }

    void test1()
    {
        string s1("hello linux!!!");
        s1.push_back('a');
        s1.push_back('b');
        s1.push_back('c');
        s1.push_back('d');
        s1.push_back('e');
        s1.push_back('f');

        for (size_t i = 0; i < s1.size(); i ++ )
        {
            cout << s1[i] << ' ';
        }
        cout << endl;

        string s2(s1);
        s2.append(" hello");
        s2.append(6, 'o');
        s2.append(s1);
        cout << s2 << endl;

        string s3("opopop");
        cout << s3 << endl;
        s3 = s2;
        cout << s3 << endl;
    }

    void test2()
    {
        string s1("hello linux !!!");
        cout << s1 << endl;
        s1.insert(0, "pppppp");
        cout << s1 << endl;

        string s2(s1);
        cout << s2 << endl;
        s2.erase(0, 6);
        cout << s2 << endl;
        s2.insert(5, " nishishabima");
        cout << s2 << endl;

        string s3;
        s3 = s2;
        cout << s3 << endl;
        s3 += " gubaby";
        cout << s3 << endl;
        s3.erase(1);
        cout << s3 << endl;
    }

    void test3()
    {
        string s1(10, 'a');
        cout << s1 << endl;
        s1.insert(6, "hello linux !!! ");
        cout << s1 << endl;
        s1.erase(s1.find("linux", 2), 6);
        cout << s1 << endl;

        string s2("hello linux");
        cout << s2 << endl;
        cout << (s1 < s2) << endl;
        cout << (s1 > s2) << endl;
        cout << (s1 == s2) << endl;
        cout << (s1 != s2) << endl;
    }
}
